package cn.wolfcode.transfer.service.impl;

import cn.wolfcode.transfer.domain.Activity;
import cn.wolfcode.transfer.domain.ActivityItem;
import cn.wolfcode.transfer.domain.Choice;
import cn.wolfcode.transfer.mapper.ActivityItemMapper;
import cn.wolfcode.transfer.mapper.ActivityMapper;
import cn.wolfcode.transfer.mapper.ChoiceMapper;
import cn.wolfcode.transfer.qo.QueryObject;
import cn.wolfcode.transfer.service.IActivityService;
import cn.wolfcode.transfer.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ActivityServiceImpl implements IActivityService {

    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ActivityItemMapper activityItemMapper;
    @Autowired
    private ChoiceMapper choiceMapper;


    @Override
    public void save(Activity activity) {
        activityMapper.insert(activity);
    }

    @Override
    public void delete(Long id) {
        activityMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Activity activity) {
        activityMapper.updateByPrimaryKey(activity);
    }

    @Override
    public List<ActivityItem> get(Long id) {
        return activityItemMapper.selectByActivityIdKey(id);
    }

    @Override
    public Activity getTitle(Long id) {
        Activity activity = activityMapper.selectByPrimaryKey(id);
        boolean x = choiceMapper.pollStatus(id, UserContext.getCurrentUser().getId()) .size() == 0;
        System.out.println(x);
        activity.setPollStatus(x);
        return activity;
    }

    @Override
    public List<Activity> listAll() {
        return activityMapper.selectAll();
    }

    @Override
    public PageInfo<Activity> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize()); //对下一句sql进行自动分页
        List<Activity> activitys = activityMapper.selectForList(qo); //里面不需要自己写limit
        for (Activity activity : activitys) {
            boolean x = choiceMapper.pollStatus(activity.getId(), UserContext.getCurrentUser().getId()).size() == 0;
            System.out.println(x);
            activity.setPollStatus(x);
        }
        return new PageInfo<Activity>(activitys);
    }

    @Override
    public void poll(Long[] optionsRadios,Long id) {
        for (Long optionsRadio : optionsRadios) {
            Choice choice = new Choice();
            choice.setActivityItemId(optionsRadio);
            choice.setCreateTime(new Date());
            choice.setEmployeeId(UserContext.getCurrentUser().getId());
            choiceMapper.insert(choice);
        }
        List<ActivityItem> activityItems = activityItemMapper.selectByActivityIdKey(id);
        for (ActivityItem activityItem : activityItems) {
            List<Choice> byactivityItemId = choiceMapper.getByActivityItemId(activityItem.getId());
            activityItem.setNum(byactivityItemId.size());
            activityItemMapper.updateNsum(activityItem);
        }

    }
}
